-- 创建主窗口
rollout RenamerRollout "批量重命名工具" width:300 height:250
(
    -- 控件定义
    label lblPrefix "前缀:" pos:[15,15]
    edittext edtPrefix "" pos:[60,12] width:100 height:18
    label lblSuffix "后缀:" pos:[15,45]
    edittext edtSuffix "" pos:[60,42] width:100 height:18
    
    checkbox chkSelectedOnly "仅重命名选中对象" pos:[15,75]
    checkbox chkKeepOriginalNumbers "保留原始编号" pos:[15,100]
    
    button btnExecute "执行重命名" pos:[15,150] width:120 height:30
    progressbar pbProgress "进度条" pos:[15,190] width:270 height:10
    
    -- 重命名逻辑
    on btnExecute pressed do
    (
        try (
            local prefix = edtPrefix.text
            local suffix = edtSuffix.text
            local objs = if chkSelectedOnly.checked then selection else objects
            local counter = 1
            
            pbProgress.value = 0
            local total = objs.count
            
            for obj in objs do
            (
                local originalName = obj.name
                local newName = ""
                
                -- 保留原始编号逻辑
                if chkKeepOriginalNumbers.checked then 
                (
                    local numMatch = filterString originalName "0123456789"
                    local numbers = ""
                    for c in numMatch do numbers += c
                )
                else
                (
                    numbers = counter as string
                )
                
                -- 构建新名称
                newName = prefix + numbers + suffix
                obj.name = newName
                
                counter += 1
                pbProgress.value = (100.0 * counter / total)
            )
            
            messageBox "重命名完成！" title:"提示"
        )
        catch (
            messageBox ("错误: " + getCurrentException()) title:"错误"
        )
    )
)

-- 创建浮动窗口
try(destroyDialog RenamerRollout)catch()
createDialog RenamerRollout
